<?php

namespace App\Http\Controllers;

use App\Http\Common\JWT;
use App\Models\AdminUser;
use Illuminate\Http\Request;

class AdminUsersController extends Controller
{

    public function register(Request $request)
    {
        try {
            $username = $request->input('username');
            $password = $request->input('password');
            $result = AdminUser::register($username, $password);
            if ($result['status']) {
                return makeResponse([], $result['message'], 201);
            } else {
                return makeResponse([], $result['message'], 500);
            }
        } catch (\Exception $exception) {
            return makeResponse([], $exception->getMessage() . ' [LINE]:' . $exception->getLine(), 500);
        }
    }

    /**
     * @OA\Post(
     *     path="/admin/login",
     *     operationId="/admin/login",
     *     summary="登陆",
     *     description="登陆 --evan",
     *     tags={"后台"},
     *     @OA\Parameter(
     *         name="username",
     *         in="query",
     *         description="username",
     *         required=true,
     *         @OA\Schema(type="string")
     *     ),
     *     @OA\Parameter(
     *         name="password",
     *         in="query",
     *         description="password",
     *         required=true,
     *         @OA\Schema(type="string")
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="success",
     *         @OA\JsonContent()
     *     ),
     *     @OA\Response(
     *         response="400",
     *         description="Error: Bad request. When required parameters were not supplied.",
     *     ),
     * )
     */
    public function login(Request $request)
    {
        try {
            $username = $request->input('username');
            $password = $request->input('password');
            $result = AdminUser::login($username, $password);
            $token_date = array(
                "iss" => "admin",          //该JWT的签发者
                "iat" => time(),        //签发时间
                "name" => $username,
                "pass" => $password,
//                "exp" => 1535974630,        //过期时间
//                "nbf" => 1535967430,         //该时间之前不接收处理该Token
//                "sub" => "www.admin.com",   //面向的用户
                "jti" => env('JWT_CODE')   //该Token唯一标识
            );
            $token = JWT::getToken($token_date);
            if ($result['status']) {
                return makeResponse(['token' => $token], '登陆成功', 200);
            } else {
                return makeResponse([], $result['message'], 500);
            }
        } catch (\Exception $exception) {
            return makeResponse([], $exception->getMessage() . ' [LINE]:' . $exception->getLine(), 500);
        }
    }

// 获取用户信息
    public function userInfo(Request $request)
    {
//        dd($request->get('user'));
        dd($request->input('user'));

        $data = $this->getUserInfo($request);
        return $data;
    }

}
